package com.pig4cloud.pigx.common.security.component.exceptionProcessed;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.pig4cloud.pigx.common.security.constant.CommonConstants;
import com.pig4cloud.pigx.common.security.exception.PigxAuth2Exception;
import lombok.SneakyThrows;

/**
 * @description: OAuth2 异常格式化
 * @Author 兔子不吃窝边曹
 * @create 2022/8/8 10:16
 **/
public class PigxAuth2ExceptionSerializer extends StdSerializer<PigxAuth2Exception> {

    public PigxAuth2ExceptionSerializer() {
        super(PigxAuth2Exception.class);
    }

    @Override
    @SneakyThrows
    public void serialize(PigxAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {
        gen.writeStartObject();
        gen.writeObjectField("code", CommonConstants.FAIL);
        gen.writeStringField("msg", value.getMessage());
        gen.writeStringField("data", value.getErrorCode());
        gen.writeEndObject();
    }

}
